package com.micro.frog.fortune.executor.cwl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.micro.frog.fortune.dao.entity.CwlAnalyPo;
import com.micro.frog.fortune.dao.entity.CwlReportPo;
import com.micro.frog.fortune.dao.mapper.CwlAnalyMapper;
import com.micro.frog.fortune.dao.mapper.CwlReportMapper;
import com.micro.frog.fortune.model.base.Resp;
import com.micro.frog.fortune.model.cwl.CwlReq;
import com.micro.frog.fortune.sdk.cwl.enums.CwlReportType;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 号码报表
 *
 * @author jason.lv
 */
@Slf4j
@Service
public class CwlReportExecutor {

    @Autowired
    private CwlAnalyMapper analyMapper;
    @Autowired
    private CwlReportMapper reportMapper;

    public Resp execute(CwlReq request) {

        QueryWrapper<CwlAnalyPo> wrapper = new QueryWrapper<>();
        wrapper.eq("name", request.getName());
        List<CwlAnalyPo> pos = analyMapper.selectList(wrapper);

        if (CollUtil.isNotEmpty(pos)) {

            // 先汇总数字的出现总次数
            Map<String, List<CwlAnalyPo>> snnp = pos.stream().collect(Collectors.groupingBy(CwlAnalyPo::getNum));
            for (Entry<String, List<CwlAnalyPo>> entry : snnp.entrySet()) {
                List<CwlAnalyPo> analyPos = entry.getValue();
                CwlAnalyPo analyPo = analyPos.get(0);

                CwlReportPo po = new CwlReportPo();
                po.setName(request.getName());
                po.setType(CwlReportType.snnp);
                po.setNum(analyPo.getNum());
                po.setColor(analyPo.getColor());
                po.setIndex(-1);
                po.setFreq(CollUtil.size(analyPos));

                // 计算概率 rate 本号码出现的总次数/所有出现的号码总次数
                int deno = po.getFreq();
                int mole = CollUtil.size(pos);
                po.setRate(NumberUtil.div(deno, mole, 5));
                reportMapper.insert(po);
            }

            // 先汇总数字的出现总次数
            Map<String, List<CwlAnalyPo>> snsp = pos.stream().collect(Collectors.groupingBy(CwlAnalyPo::group));
            for (Entry<String, List<CwlAnalyPo>> entry : snsp.entrySet()) {

                List<CwlAnalyPo> analyPos = entry.getValue();
                CwlAnalyPo analyPo = analyPos.get(0);

                CwlReportPo po = new CwlReportPo();
                po.setName(request.getName());
                po.setType(CwlReportType.snsp);
                po.setNum(analyPo.getNum());
                po.setColor(analyPo.getColor());
                po.setIndex(analyPo.getIndex());
                po.setFreq(CollUtil.size(analyPos));
                // 计算概率 rate 本号码出现的总次数/所有出现的号码总次数
                int deno = po.getFreq();
                int mole = CollUtil.size(pos);
                po.setRate(NumberUtil.div(deno, mole, 5));


                reportMapper.insert(po);
            }
        }
        return Resp.generate();
    }
}

